package com.iris.smartclass.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iris.smartclass.annotation.AuthCheck;
import com.iris.smartclass.common.BaseResponse;
import com.iris.smartclass.common.DeleteRequest;
import com.iris.smartclass.common.ErrorCode;
import com.iris.smartclass.common.ResultUtils;
import com.iris.smartclass.exception.BusinessException;
import com.iris.smartclass.exception.ThrowUtils;
import com.iris.smartclass.model.dto.student.*;
import com.iris.smartclass.model.entity.Student;
import com.iris.smartclass.model.entity.User;
import com.iris.smartclass.model.vo.ClassDataVO;
import com.iris.smartclass.model.vo.StudentVO;
import com.iris.smartclass.service.StudentService;
import com.iris.smartclass.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RequestMapping("/student")
@Slf4j
@RestController
public class StudentController {

    @Resource
    private StudentService studentService;

    @Resource
    private UserService userService;



    @GetMapping("/classData")
    public BaseResponse<ClassDataVO> getClassData(HttpServletRequest request) {
        ClassDataVO classData = studentService.getClassData(request);
        return ResultUtils.success(classData);
    }


    /**
     * 创建学生
     *
     * @param studentAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck( mustRole = "teacher")
    public BaseResponse<Long> addStudent(@RequestBody StudentAddRequest studentAddRequest, HttpServletRequest request) {
        if (studentAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Student student = new Student();
        BeanUtils.copyProperties(studentAddRequest, student);
        User loginUser = userService.getLoginUser(request);
        student.setUuid(loginUser.getId());
        boolean result = studentService.save(student);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(student.getId());
    }

    /**
     * 删除学生
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck( mustRole = "teacher")
    public BaseResponse<Boolean> deleteStudent(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = studentService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新学生
     *
     * @param studentUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck( mustRole = "teacher")
    public BaseResponse<Boolean> updateStudent(@RequestBody StudentUpdateRequest studentUpdateRequest,
                                            HttpServletRequest request) {
        if (studentUpdateRequest == null || studentUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Student student = new Student();
        BeanUtils.copyProperties(studentUpdateRequest, student);
        boolean result = studentService.updateById(student);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 根据 id 获取学生（仅管理员）
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Student> getStudentById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Student student = studentService.getById(id);
        ThrowUtils.throwIf(student == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(student);
    }

    /**
     * 根据 id 获取包装类
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<StudentVO> getStudentVOById(long id, HttpServletRequest request) {
        BaseResponse<Student> response = getStudentById(id, request);
        Student student = response.getData();
        return ResultUtils.success(studentService.getStudentVO(student));
    }

    /**
     * 分页获取学生列表（仅管理员）
     *
     * @param studentQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = "teacher")
    public BaseResponse<Page<Student>> listStudentByPage(@RequestBody StudentQueryRequest studentQueryRequest,
                                                   HttpServletRequest request) {
        long current = studentQueryRequest.getCurrent();
        long size = studentQueryRequest.getPageSize();
        Page<Student> studentPage = studentService.page(new Page<>(current, size),
                studentService.getQueryWrapper(studentQueryRequest,request));
        return ResultUtils.success(studentPage);
    }

    /**
     * 分页获取学生封装列表
     *
     * @param studentQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<StudentVO>> listStudentVOByPage(@RequestBody StudentQueryRequest studentQueryRequest,
                                                             HttpServletRequest request) {
        if (studentQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = studentQueryRequest.getCurrent();
        long size = studentQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Student> studentPage = studentService.page(new Page<>(current, size),
                studentService.getQueryWrapper(studentQueryRequest,request));
        Page<StudentVO> studentVOPage = new Page<>(current, size, studentPage.getTotal());
        List<StudentVO> studentVO = studentService.getStudentVO(studentPage.getRecords());
        studentVOPage.setRecords(studentVO);
        return ResultUtils.success(studentVOPage);
    }

    @PostMapping("/send")
    public BaseResponse<Boolean> StudentSendPhone(@RequestBody StudentSendCodeRequest studentSendCodeRequest){
        return ResultUtils.success(studentService.sendPhoneCode(studentSendCodeRequest));
    }

    @PostMapping("/login")
    public BaseResponse<String> StudentLoginByPhone(@RequestBody StudentLoginRequest studentLoginRequest) {
        String token = studentService.StudentLoginByPhone(studentLoginRequest);
        return ResultUtils.success(token);
    }


}
